var _util = require('util/util.js');
var _cart_service = require('service/cart-service.js');
var listTemplate = require('./index.string');

require('page/common/header/index.js');

var myCart = {
    listDate : {
        itemList    : {},
        productList : {},
        mapList     : {}

    },
    init: function () {
        
        this.loadAccountInfo();
        this.quantityChange();
        this.bindEvents();
        return this;
    },
    bindEvents: function () {
    },
    quantityChange : function(){
        var itemId;
        var qty;
        // 获取input输入框的值
        //var value=document.getElementById("quantity").value;
        // 输出值
        //console.log(value);
        //var itemId ;
        //var qty ;
        var divFa = document.getElementById('divFa');
        divFa.addEventListener('focusout',(e)=>{
          if(e.target.nodeName == 'INPUT'){
            itemId=e.target.name;
            qty=e.target.value;
          }
        _cart_service.updateItemQtyFromCart(
            itemId,
            qty,
            function(res){
                _this.listDate.itemList = res;
                _this.listDate.productList = res.itemMap;
                _this.listDate.mapList = Object.values(res.itemMap);

                var result = _util.renderHtml(listTemplate, {ListData : _this.listDate});
                $('#cart').html(result);
            },
            function(errMsg){
                console.log(errMsg);
            }
        )})
    },
    loadAccountInfo : function(){
        var id = _util.getURLParam('id');
        var route = _util.getURLParam('route');
        var _this = this;
        if(id === 'addToCart'){
            _cart_service.addItemToCart   (
                route,
                function(res){
                    _cart_service.viewCart(
                        function(res){      
                            _this.listDate.itemList = res;
                            _this.listDate.productList = res.itemMap;
                            _this.listDate.mapList = Object.values(res.itemMap);

                            var result = _util.renderHtml(listTemplate, {ListData : _this.listDate});
                            $('#cart').html(result);
                            _this.quantityChange();
                    },
                        function (errMsg){
                        }
                    )
                },
                function(errMsg) {
                    // 对应Thymeleaf中账号未登录的相关逻辑
                    console.log('errMsg...');
                }
            );
        }
        if(id === 'myCart'){
            _cart_service.viewCart   (
                function(res){
                    _cart_service.viewCart(
                        function(res){        
                            _this.listDate.itemList = res;
                            _this.listDate.productList = res.itemMap;
                            _this.listDate.mapList = Object.values(res.itemMap);

                            var result = _util.renderHtml(listTemplate, {ListData : _this.listDate});
                            console.log("list"+result);
                            $('#cart').html(result);
                            _this.quantityChange();
                        },
                        function (errMsg){
                        }
                    )
                },
                function(errMsg) {
                    // 对应Thymeleaf中账号未登录的相关逻辑
                    console.log('errMsg...');
                }
            );
        }
        if(id === 'deleteItemFromCart'){
            _cart_service.deleteItemFromCart   (
                route,
                function(res){
                    _cart_service.viewCart(
                        function(res){
                            _this.listDate.itemList = res;
                            _this.listDate.productList = res.itemMap;
                            _this.listDate.mapList = Object.values(res.itemMap);

                            var result = _util.renderHtml(listTemplate, {ListData : _this.listDate});
                            console.log("list"+result);
                            $('#cart').html(result);
                        },
                        function (errMsg){
                        }
                    )
                },
                function(errMsg) {
                    // 对应Thymeleaf中账号未登录的相关逻辑
                    console.log('errMsg...');
                }
            );
        }
    }
}
module.exports = myCart.init();